﻿using System;

namespace OnePiece.Core.Extension
{
    public static class ConvertExtension
    {
        public static int ToInteger(this string item)
        {
            try
            {
                return string.IsNullOrEmpty(item) ? 0 : Convert.ToInt32(item);
            }
            catch (System.Exception)
            {
                return 0;
            }
        }

        public static int? ToIntegerNullable(this string item)
        {
            try
            {
                var r = string.IsNullOrEmpty(item) ? (int?)null : Convert.ToInt32(item);
                if (r == null || r < 0)
                    r = null;
                return r;
            }
            catch (System.Exception)
            {
                return null;
            }
        }

        public static double ToDouble(this string item)
        {
            try
            {
                return string.IsNullOrEmpty(item) ? 0 : System.Convert.ToDouble(item);
            }
            catch (System.Exception)
            {
                return 0;
            }
        }

        public static double? ToDoubleNullable(this string item)
        {
            try
            {
                var r = string.IsNullOrEmpty(item) ? (double?)null : System.Convert.ToDouble(item);
                if (r == null || r < 0)
                    r = null;
                return r;

            }
            catch (System.Exception)
            {
                return null;
            }
        }

        public static short ToShort(this string item)
        {
            try
            {
                return string.IsNullOrEmpty(item) ? (short)0 : System.Convert.ToInt16(item);
            }
            catch (System.Exception)
            {
                return 0;
            }
        }

        public static short? ToShortNullable(this string item)
        {
            try
            {
                var r = string.IsNullOrEmpty(item) ? (short?)null : System.Convert.ToInt16(item);
                if (r == null || r < 0)
                    r = null;
                return r;

            }
            catch (System.Exception)
            {
                return null;
            }
        }

        public static DateTime? ToDateTime(this string item)
        {
            try
            {
                return string.IsNullOrEmpty(item) ? (DateTime?)null : System.Convert.ToDateTime(item);
            }
            catch (System.Exception)
            {
                return null;
            }
        }

        public static bool AsBoolenIfNullOrEmpty(this string item)
        {
            try
            {
                return string.IsNullOrEmpty(item) ? true : false;
            }
            catch (System.Exception)
            {
                return false;
            }
        }

        public static bool ToBoolean(this string item)
        {
            try
            {
                var returnValue = false;
                if (!string.IsNullOrEmpty(item))
                {
                    var stringValue = (item.ToUpper() == "FALSE") ? 0 : 1;
                    returnValue = System.Convert.ToBoolean(stringValue);
                }
                return returnValue;
            }
            catch (System.Exception)
            {
                return false;
            }
        }

        public static string ToShortDateTime(this DateTime? item)
        {
            try
            {
                return (item == null) ? "" : string.Format("{0:MM/dd/yyyy}", item);
            }
            catch (System.Exception)
            {
                return "";
            }
        }


        public static T Cast<T>(object obj, T type)
        {
            return (T)obj;
        }


    }
}